#include "../Include/BitmapMasked.h"

// This lib is needed for TransparentBlt.
#pragma comment( lib, "Msimg32.lib" )

void BitmapMasked::Render( HDC hBBDC, HDC hSprDC, INT x, INT y )
{
	HGDIOBJ oldObj = SelectObject( hSprDC, m_hBitmap );
	LONG w = GetWidth();
	LONG h = GetHeight();

	TranslatePosition( x, y );

	TransparentBlt( hBBDC, x, y, w, h, hSprDC, 0, 0, w, h, m_cKey ); 

	SelectObject( hSprDC, oldObj );
}

void BitmapMasked::Render( HDC hBBDC, HDC hSprDC, INT x, INT y, INT w, INT h, INT srcX, INT srcY )
{
	HGDIOBJ oldObj = SelectObject( hSprDC, m_hBitmap );

	TranslatePosition( x, y );

	TransparentBlt( hBBDC, x, y, w, h, hSprDC, srcX, srcY, w, h, m_cKey ); 

	SelectObject( hSprDC, oldObj );
}

Pulse::BOOL BitmapMasked::TestPoint( INT x, INT y )
{
	// General rect collision test first
	if ( !Bitmap::TestPoint( x, y ) )
		return FALSE;

	Pulse::BOOL returnVal;

	// Do further pixel checking
	UINT  pixPos = x * y;

	UCHAR *pPixelBits = (UCHAR*)m_bmInfo.bmBits;

	HDC hDC = CreateCompatibleDC( NULL );
	HBITMAP oldBitmap = (HBITMAP)SelectObject( hDC, m_hBitmap );

	returnVal = GetPixel( hDC, x, y ) != this->m_cKey;

	SelectObject( hDC, oldBitmap );
	DeleteDC( hDC );

	return returnVal;

}